package com.iwop.moto.controller.web;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.controller.BaseController;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.FRecommendService;
import com.iwop.moto.utils.stringUtil.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by mobom on 2016/11/1.
 */
@Controller
public class FRecommendController extends BaseController {

    @Autowired
    private FRecommendService fRecommendService;

    @RequestMapping(value="/api/reommend/add")
    @ResponseBody
    public ResultModel addRecommend(){
        try {
            SuperRecordModel superRecordModel=this.getSRModel();
            superRecordModel.put("sid", ObjectId.UUID());
            superRecordModel.put("status_flag", "1");
            SuperRecordModel recommend = fRecommendService.getRecommend(superRecordModel);
            if(recommend!=null&&recommend.getString("status_falg").equals("0")){
                fRecommendService.deleteRecommend(superRecordModel);
                fRecommendService.insertRecommend(superRecordModel);
            }else if(recommend!=null&&recommend.getString("status_falg").equals("1")){
                return getSucceed(ErrCode.SUCCEED);
            }else {
                fRecommendService.insertRecommend(superRecordModel);
            }
            return getSucceed(ErrCode.SUCCEED);
        }catch (Exception e){
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }
}
